package com.skyline.user.service.impl;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.skyline.user.dao.AccountInfoDao;
import com.skyline.user.model.User;
import com.skyline.user.service.AccountInfoService;

//TODO:加注释
@Service("accountInfoService")
public class AccountInfoServiceImpl implements AccountInfoService {
	@Autowired
	private AccountInfoDao accountInfoDao;

	@Override
	public void modifySecurityInfo(Long userid, Integer authority, String securityQuestion, String securityAnswer, String securityEMail) {
		accountInfoDao.updateSecurityInfo(userid, authority, securityQuestion, securityAnswer, securityEMail);

	}

	@Override
	public boolean modifyPassword(String email, String oldPassword, String newPassword) {
		oldPassword = encodePassword(oldPassword);
		newPassword = encodePassword(newPassword);
		User user = accountInfoDao.queryAccountInfoByEmailAndPswd(email, oldPassword);
		if (user == null) {
			return false;
		} else {
			accountInfoDao.updatePassword(user.getId(), newPassword);
			return true;
		}
	}

	@Override
	public void restPassword(Long  userId,String newPassword){
		newPassword = encodePassword(newPassword);
		accountInfoDao.updatePassword(userId, newPassword);
	}
	
	@Override
	public User getAccountInfoByUserID(Long userid) {
		return accountInfoDao.queryAccountInfoByUserID(userid);
	}

	@Override
	public User getAccountInfoByEmail(String email) {
		return accountInfoDao.queryAccountInfoByEmail(email);
	}

	@Override
	public User getSecurityQuestion(String email) {
		return accountInfoDao.querySecurityQuestion(email);
	}

	@Override
	public boolean checkSecurityAnswer(Long userid, String securityAnswer) {
		return accountInfoDao.existSecurityAnswer(userid, securityAnswer);
	}

	@Override
	public void sendSecurityEmail(String email, String securityEmail) {

	}

	@Override
	public boolean checkSecurityEmail(String token) {
		return false;
	}

	@Override
	public User getAccountInfoByEmailAndPswd(String email, String password) {
		password = encodePassword(password);
		return accountInfoDao.queryAccountInfoByEmailAndPswd(email, password);
	}

	private String encodePassword(String password) {
		return DigestUtils.md5Hex(password);
	}

}
